package com.prj.ufdm.cloud.svr.gateway.bpo._impl;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.prj.ufdm.cloud.svr.gateway.bpo.SysFunctionBPO;
import com.prj.ufdm.cloud.svr.gateway.bpo.SysUserRedisBPO;
import com.prj.ufdm.cloud.svr.gateway.config.RedisService;
import com.prj.ufdm.cloud.svr.gateway.constant.WebSecurityConstants;
import com.prj.ufdm.cloud.svr.gateway.model.SysFunction;
import com.prj.ufdm.cloud.svr.gateway.model.SysUser;


/**
 * 系统用户管理
 */
@Service 
public class SysUserRedisBPOImpl  implements SysUserRedisBPO {
	
	@Autowired
	RedisService redisService;
	
	@Autowired
	SysFunctionBPO sysFunctionBPO;

	@Override
	public void modifyByRedis(SysUser sysUser) throws Exception {
		
		// 如果不允许一个账号同时登录，删除所有缓存信息
		if(!"1".equals(sysUser.getEnableParallelLogin())) {
			Set<String> keys = redisService.getKeys(WebSecurityConstants.REDIS_MAP_KEY_TOKEN_USERINFO+":*");
			for(String key:keys) {
				Object redisObject = redisService.get(key);
				SysUser redisSysUser = JSON.parseObject(redisObject!=null?redisObject.toString():null, new TypeReference<SysUser>(){}.getType());
				if(redisSysUser!=null && redisSysUser.getId()!=null  &&  redisSysUser.getId().equals(sysUser.getId())) {
					redisService.del(key);
				}
			}
			/**
			Set<Object> setAllUserToken =  redisService.hmget(WebSecurityConstants.REDIS_MAP_KEY_TOKEN_USERINFO).keySet();
			if(setAllUserToken!=null) {
				for(Object userToken : setAllUserToken) {
					 SysUser tokenUser = (SysUser) redisService.hget(WebSecurityConstants.REDIS_MAP_KEY_TOKEN_USERINFO, userToken.toString());
					 if(sysUser.getId().equals(tokenUser.getId())) {
						 redisService.hdel(WebSecurityConstants.REDIS_MAP_KEY_TOKEN_USERINFO, userToken);
					 }
				}
			}
			redisService.hdel(WebSecurityConstants.REDIS_MAP_KEY_USERID_USERINFO, sysUser.getId());
			redisService.hdel(WebSecurityConstants.REDIS_MAP_KEY_USERID_FUNCTION, sysUser.getId());
			*/
		}
		
		/**
		// 设置 “TOKEN：用户信息”
		redisService.hset(WebSecurityConstants.REDIS_MAP_KEY_TOKEN_USERINFO, sysUser.getToken(), sysUser,WebSecurityConstants.TOKEN_VALID_MINUTES*60);
		// 设置 “USERID：用户信息”
		redisService.hset(WebSecurityConstants.REDIS_MAP_KEY_USERID_USERINFO, sysUser.getId(), sysUser,WebSecurityConstants.TOKEN_VALID_MINUTES*60);
		// 设置 “USERID：用户授权功能”
		List<SysFunction> listSysFunction = sysFunctionBPO.doGetFunctionListByUserId(sysUser.getId());
		redisService.hset(WebSecurityConstants.REDIS_MAP_KEY_USERID_FUNCTION, sysUser.getId(), listSysFunction,WebSecurityConstants.TOKEN_VALID_MINUTES*60);
		*/
		
		// 设置 “TOKEN：用户信息”
		redisService.set(WebSecurityConstants.REDIS_MAP_KEY_TOKEN_USERINFO+":"+sysUser.getToken(), sysUser,WebSecurityConstants.TOKEN_VALID_MINUTES*60);
		// 设置 “USERID：用户信息”
		redisService.set(WebSecurityConstants.REDIS_MAP_KEY_USERID_USERINFO+":"+sysUser.getId(), sysUser);
		// 设置 “USERID：授权功能”
		List<SysFunction> listUserSysFunction = sysFunctionBPO.doGetFunctionListByUserId(sysUser.getId());
		redisService.set(WebSecurityConstants.REDIS_MAP_KEY_USERID_FUNCTION+":"+sysUser.getId(), listUserSysFunction);
	}

}
